import { componentsFnMap } from '../../config';
const settings = {
  idPrefix: ``,
  labelName: 'Input输入框',
  componentNameShow: 'Input输入框', // 负责显示组件的名称
  busname: 'baseInput', // 组件的唯一标识，不可修改，在业务组件中，它用于对应业务组件的校验规则src\common\componentRules\index.js
  componentName: 'baseInput', // 组件标签名称，不可修改,也用作vue中v-for的key值组成,以base开头，用于与业务组件区分。
  minLength: '',
  maxLength: ''
};
componentsFnMap[settings.componentName] = {
  getHtmlStr(options = {},otherOptions={otherComponentSettings:``,showSetting:''}) {
    let {
      boxId,
      formBoxId,
      inputType = '正常',
      componentValue = '',
      idPrefix = '',
      formName = '',
      formItemName = '',
      focusIndex = '',
      labelName = '标签名',
      labelWidth = '80px',
      placeholder = '不能为空'
    } = options;
    let {
      minLength = '',
      maxLength = '',
      needTooltip = '0',
      colMd = '6',
      show = true,
      disabled
    } = options;
    let id = `${idPrefix}`;
    const { busname, componentName } = settings;
    let {otherComponentSettings,showSetting} = otherOptions
    return `
            <el-col v-bind="currentLayoutOptions" id="${boxId}" ${showSetting}>
              <el-form-item prop="${formItemName}" label="${labelName}">
                <el-input
                  v-model="${formName}.props.${formItemName}"
                  placeholder="${placeholder}"${otherComponentSettings}
                  focusIndex = "${formBoxId}#${formItemName}"
                />
              </el-form-item>
            </el-col>    
       `;
  },
  getFormItemRules(item) {
    let formItemRulesStr = ``;
    // 根据item上的信息拼装formItemRulesStr代码
    formItemRulesStr += `
            [
              {trigger:'blur',required:${item.required},validator:async (rule, value, callback)=>{
                  console.log(rule, value,)
                  if (rule.required && !value) {
                    return callback(new Error('请输入必填项'))
                  }
                  callback();// 校验通过
                }},
            `;
    if (item.minLength || item.maxLength) {
      let message;
      let minStr = ``;
      let maxStr = ``;
      if (item.minLength === item.maxLength) {
        message = `长度必须是${item.maxLength}`;
        minStr = `,min:${item.minLength}`;
        maxStr = `,max:${item.maxLength}`;
      } else {
        if (item.minLength && !item.maxLength) {
          message = `长度最小是${item.minLength}`;
          minStr = `,min:${item.minLength}`;
        }
        if (!item.minLength && item.maxLength) {
          message = `长度最大是${item.maxLength}`;
          maxStr = `,max:${item.maxLength}`;
        }
        if (item.minLength && item.maxLength) {
          message = `长度应该在${item.minLength}和${item.maxLength}之间`;
          minStr = `,min:${item.minLength}`;
          maxStr = `,max:${item.maxLength}`;
        }
      }
      formItemRulesStr += `{trigger:'blur',message:'${message}'${minStr}${maxStr}},`;
    }
    formItemRulesStr += `
            ]`;
    return formItemRulesStr
  }
};

export default settings;
